﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Data;

namespace GetHistory
{
    class CreateExcelDoc
    {
        private Microsoft.Office.Interop.Excel.Application app = null;
        private Microsoft.Office.Interop.Excel.Workbook workbook = null;
        private Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
        private Microsoft.Office.Interop.Excel.Range workSheet_range = null;

        public CreateExcelDoc(TabControl history)
        {
            CreateDoc(history);
        }

        public void CreateDoc(TabControl history)
        {
            try
            {
                app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = true;
                workbook = app.Workbooks.Add();
                //worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
                
                foreach (TabPage tp in history.TabPages)
                {
                    worksheet = workbook.Worksheets.Add();
                    worksheet.Name = tp.Name;
                    DataSet ds = ((HistoryData)tp.Controls[0]).Data;

                    int r = 1;
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                        {
                            worksheet.Cells[r, i+1] = row[i].ToString();
                        }
                        r++;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
            }
        }
    }
}